/**
  * @Description: WoShop
  * @Author: Parker
  * @Copyright: 武汉一一零七科技有限公司©版权所有
  * @Link: www.s1107.com
  * @Contact: QQ:2487937004
  */
const Fly = require('flyio/dist/npm/wx')
const fly = new Fly



import { SERVERTAPI} from '../config/index'
import {showToast} from '../utils/index'
fly.config.timeout = 20000
fly.config.baseURL = SERVERTAPI;


fly.interceptors.request.use(request => {

    request.body.token = uni.getStorageSync('token')
    request.body.device_token = uni.getStorageSync('devicetoken')

    return request
})

fly.interceptors.response.use(
    (response) => {
        uni.hideLoading()
        uni.stopPullDownRefresh()
        if (response.data && response.data.status == 200) {
            // logout

            return response.data
        }

        if (response.data && response.data.status === 402) {
            uni.setStorageSync('token', '');

            uni.navigateTo({
                url: '/pagesB/login/login'
            })
            return response.data
        }
		if (response.data && response.data.status == 400) {
			 showToast(response.mess)
		
		    // utils.showToast(`status:${response.data.status} -- ${response.data.mess}`)
		    return response.data
		}
        if (response.data && response.data.status == 300) {
            // utils.showToast(`status:${response.data.status} -- ${response.data.mess}`)
            return response.data
        }
        if (response.data && response.data.status != 200) {
           showToast(`${response.data.mess}`)
            return response.data
        }
          if (response.data  && response.data.status == 500) {

              return response.data
          }

    },
    (err) => {
        uni.hideLoading()
        uni.stopPullDownRefresh()
        if (err.status == 0) {
            //NetworkError网络错误
           showToast('网络错误')
            return err
        }
        console.log(JSON.stringify(err))
        if (err.status == 500) {
             //ServerError服务器错误
              showToast('服务器错误')
            return err
        }

    }
)

export default fly
